package com.ryl.yomall.list.controller;

import com.ryl.yomall.list.service.ListService;
import com.ryl.yomall.model.common.result.Result;
import com.ryl.yomall.model.list.Goods;
import com.ryl.yomall.model.list.SearchParam;
import com.ryl.yomall.model.list.SearchResponseVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.elasticsearch.core.ElasticsearchRestTemplate;
import org.springframework.web.bind.annotation.*;

/**
 * @author ryl
 */
@RestController
@RequestMapping("api/list")
public class ListApiController {

    @Autowired
    private ElasticsearchRestTemplate restTemplate;
    @Autowired
    private ListService listService;
    @PostMapping
    public SearchResponseVo list(@RequestBody SearchParam searchParam){
        return listService.list(searchParam);
    }

    /**
     *
     * @return
     */
    @GetMapping("inner/createIndex")
    public Result createIndex() {
        //创建库
        restTemplate.createIndex( Goods.class);
        // 映射 表 字段 指定字段类型
        restTemplate.putMapping(Goods.class);
        return Result.ok();
    }
    /**
     * 商品上架 到ES库
     */
    @GetMapping("/onSale/{skuId}")
    public Result onSale(@PathVariable Long skuId){
        listService.onSale(skuId);
        return Result.ok ();
    }
    /**
     * 商品下架 ,从索引库删除
     */
    @GetMapping("/cancelSale/{skuId}")
    public Result cancelSale(@PathVariable Long skuId){
        listService.cancelSale(skuId);
        return Result.ok ();
    }
    /**
     * 更新热度
     */
    @GetMapping("/inner/hotScore/{skuId}")
    public Result hotScore(@PathVariable Long skuId){
        listService.hotScore(skuId);
        return Result.ok ();
    }
}
